/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Controller.ControlData;
import Data.PasienData;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;

/**
 *
 * @author a9uszT
 */
public class Registrasi_baru extends javax.swing.JFrame {

    /**
     * Creates new form Registrasi_baru
     */
    public Registrasi_baru() {
        initComponents();
        ButtonGroup BG = new ButtonGroup();
        BG.add(L_radio);
        BG.add(P_radio);
        GregorianCalendar gc=new GregorianCalendar();
        tanggalLahir_DC.setDate(gc.getTime());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        idPasien_TF = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        namaPasien_TF = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        alamat_TF = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        namaOrtu_TF = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        noTelpOrtu_TF = new javax.swing.JTextField();
        batal_button = new javax.swing.JButton();
        simpan_button = new javax.swing.JButton();
        keluar_button = new javax.swing.JButton();
        tanggalLahir_DC = new com.toedter.calendar.JDateChooser();
        P_radio = new javax.swing.JRadioButton();
        L_radio = new javax.swing.JRadioButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        idpetugasTF = new javax.swing.JTextField();
        keluar_button1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBackground(new java.awt.Color(102, 255, 102));

        jPanel3.setBackground(new java.awt.Color(102, 255, 102));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Pasien Baru", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 0, 24))); // NOI18N
        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel5.setText("id Pasien");
        jPanel3.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(84, 60, -1, -1));

        idPasien_TF.setEnabled(false);
        jPanel3.add(idPasien_TF, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 60, 75, -1));

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel6.setText("Nama pasien");
        jPanel3.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(84, 88, -1, -1));

        namaPasien_TF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                namaPasien_TFKeyReleased(evt);
            }
        });
        jPanel3.add(namaPasien_TF, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 90, 213, -1));

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel7.setText("Alamat");
        jPanel3.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(84, 121, -1, -1));
        jPanel3.add(alamat_TF, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 120, 400, -1));

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel8.setText("Tanggal Lahir");
        jPanel3.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(84, 158, -1, -1));

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel9.setText("Jenis Kelamin");
        jPanel3.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(84, 196, -1, -1));

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel10.setText("Nama Orang tua");
        jPanel3.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 270, -1, -1));
        jPanel3.add(namaOrtu_TF, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 270, 213, -1));

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel11.setText("No telp Orang Tua");
        jPanel3.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 310, -1, -1));
        jPanel3.add(noTelpOrtu_TF, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 310, 213, -1));

        batal_button.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        batal_button.setText("Batal");
        batal_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                batal_buttonActionPerformed(evt);
            }
        });
        jPanel3.add(batal_button, new org.netbeans.lib.awtextra.AbsoluteConstraints(84, 377, 100, 39));

        simpan_button.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        simpan_button.setText("Simpan");
        simpan_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                simpan_buttonActionPerformed(evt);
            }
        });
        jPanel3.add(simpan_button, new org.netbeans.lib.awtextra.AbsoluteConstraints(263, 377, 100, 39));

        keluar_button.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        keluar_button.setText("Keluar");
        keluar_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                keluar_buttonActionPerformed(evt);
            }
        });
        jPanel3.add(keluar_button, new org.netbeans.lib.awtextra.AbsoluteConstraints(458, 377, 100, 39));

        tanggalLahir_DC.setDateFormatString("d MMM yyyy");
        tanggalLahir_DC.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tanggalLahir_DCMouseClicked(evt);
            }
        });
        tanggalLahir_DC.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                tanggalLahir_DCPropertyChange(evt);
            }
        });
        jPanel3.add(tanggalLahir_DC, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 160, 213, -1));

        P_radio.setBackground(new java.awt.Color(102, 255, 102));
        P_radio.setText("Perempuan");
        jPanel3.add(P_radio, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 230, -1, -1));

        L_radio.setBackground(new java.awt.Color(102, 255, 102));
        L_radio.setText("Laki - Laki");
        jPanel3.add(L_radio, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 200, -1, -1));

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));
        jPanel4.setBorder(new javax.swing.border.MatteBorder(null));
        jPanel4.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setBackground(new java.awt.Color(51, 51, 51));
        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 30)); // NOI18N
        jLabel1.setText("Sistem Informasi Imunisasi");
        jPanel4.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 10, 430, 50));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel2.setText("Puskemas Klaten Selatan");
        jPanel4.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 70, -1, 28));

        jLabel13.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel13.setText("JL. Perjuangan 12, Glodogan, Klaten Selatan");
        jPanel4.add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 110, -1, -1));

        jLabel14.setIcon(new javax.swing.ImageIcon(getClass().getResource("/picture/puskemas.jpg"))); // NOI18N
        jLabel14.setToolTipText("");
        jPanel4.add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 0, 120, 160));

        jLabel12.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel12.setText("idPetugas");
        jPanel4.add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(590, 120, -1, 30));

        idpetugasTF.setEnabled(false);
        idpetugasTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                idpetugasTFActionPerformed(evt);
            }
        });
        jPanel4.add(idpetugasTF, new org.netbeans.lib.awtextra.AbsoluteConstraints(650, 130, 44, -1));

        keluar_button1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        keluar_button1.setText("Logout");
        keluar_button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                keluar_button1ActionPerformed(evt);
            }
        });
        jPanel4.add(keluar_button1, new org.netbeans.lib.awtextra.AbsoluteConstraints(700, 130, -1, -1));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 791, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(1, 1, 1)
                            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, 790, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 790, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(0, 0, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 636, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(6, 6, 6)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 470, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void batal_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_batal_buttonActionPerformed
        emptyForm();
        idPasien_TF.requestFocus();
    }//GEN-LAST:event_batal_buttonActionPerformed

    private void simpan_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_simpan_buttonActionPerformed
        String id = idPasien_TF.getText();
        String nama = namaPasien_TF.getText();
        String alamat = alamat_TF.getText();
        Date tgl = tanggalLahir_DC.getDate();
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
        String tanggal = sdf.format(tgl);
        String jenisKel = "";
        if (L_radio.isSelected()) {
            jenisKel = "L";
        } else if (P_radio.isSelected()) {
            jenisKel = "P";
        }
        String namaOrtu = namaOrtu_TF.getText();
        String notelp = noTelpOrtu_TF.getText();
        if (jenisKel == null || id.equals("") || nama.equals("")
                || alamat.equals("") || tanggalLahir_DC == null
                || notelp.equals("") || namaOrtu.equals("")) {
            JOptionPane.showMessageDialog(rootPane, "belum lengkap,harap isi form yang masih kosong",
                    "error", JOptionPane.ERROR_MESSAGE);

        } else {
            PasienData PD = new PasienData();
            PD.setIdPasien(id);
            PD.setNamaPasien(nama);
            PD.setAlamat(alamat);
            PD.setTanggalLahir(tanggal);
            PD.setJenisKelamin(jenisKel);
            PD.setNamaOrangTua(namaOrtu);
            PD.setNotelpOrtu(notelp);
            try {
                String namaPegawai = ControlData.getKoneksi().cariPegawai(idpetugasTF.getText());
                ControlData.getKoneksi().tambahPasienBaruPrepared(PD);
                JOptionPane.showMessageDialog(rootPane, "data pasien " + nama + " terisimpan");
                emptyForm();
                this.dispose();
                RekamPasien rp = new RekamPasien();
                rp.setVisible(true);
                RekamPasien.idpasienTF.setEditable(false);
                RekamPasien.idpasienTF.setEnabled(false);
                RekamPasien.idpasienTF.setText(id);
                RekamPasien.namaPasienTF.setText(nama);
                RekamPasien.namaOrtuTF.setText(namaOrtu);
                RekamPasien.idpetugasTF.setText(idpetugasTF.getText());
                RekamPasien.namaPegawaiTF.setText(namaPegawai);
                RekamPasien.Hapus.setEnabled(true);
                RekamPasien.input_button.setEnabled(true);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(rootPane, "terjadi error " + ex.getMessage());
                Logger.getLogger(Registrasi_baru.class.getName()).log(Level.SEVERE, null, ex);
            }
        }



    }//GEN-LAST:event_simpan_buttonActionPerformed

    private void keluar_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keluar_buttonActionPerformed

        this.dispose();
        Registrasi reg = new Registrasi();
        Registrasi.idpetugasTF.setText(idpetugasTF.getText());
        reg.setVisible(true);
    }//GEN-LAST:event_keluar_buttonActionPerformed

    private void idpetugasTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_idpetugasTFActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_idpetugasTFActionPerformed

    private void keluar_button1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keluar_button1ActionPerformed
        int a = JOptionPane.showConfirmDialog(rootPane, "yakin keluar", "exit", JOptionPane.YES_NO_OPTION);
        if (a == 0) {
            JOptionPane.showMessageDialog(rootPane, "selesai ");
            this.dispose();
            Frame_petugas fp = new Frame_petugas();
            fp.dispose();
            Login_menu lm = new Login_menu();
            lm.setVisible(true);
        }
    }//GEN-LAST:event_keluar_button1ActionPerformed

    private void namaPasien_TFKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_namaPasien_TFKeyReleased
        automaticNumber();
    }//GEN-LAST:event_namaPasien_TFKeyReleased

    private void tanggalLahir_DCMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tanggalLahir_DCMouseClicked
    }//GEN-LAST:event_tanggalLahir_DCMouseClicked

    private void tanggalLahir_DCPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_tanggalLahir_DCPropertyChange
//               GregorianCalendar gc=new GregorianCalendar();
//       Date a=gc.getTime();
//        Date b=tanggalLahir_DC.getDate();
//        long selisih =a.getTime()-b.getTime();
//        System.out.println(selisih);
    }//GEN-LAST:event_tanggalLahir_DCPropertyChange
    private void emptyForm() {
        idPasien_TF.setText("");
        namaPasien_TF.setText("");
        alamat_TF.setText("");
        tanggalLahir_DC.setDate(null);
        namaOrtu_TF.setText("");
        noTelpOrtu_TF.setText("");
    }

    private void automaticNumber() {
        try {
            String code = namaPasien_TF.getText().substring(0, 1);
//            System.out.println(code);
            String a = ControlData.getKoneksi().nomorBaru(code);
            if (a.equals("null")) {
                a = "001";
            } else if (a.length() == 1) {
                a = "00" + a;
            } else if (a.length() == 2) {
                a = "0" + a;
            }
            idPasien_TF.setText(code + a);
        } catch (Exception ex) {
            Logger.getLogger(Registrasi_baru.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Registrasi_baru.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Registrasi_baru.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Registrasi_baru.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Registrasi_baru.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Registrasi_baru().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton L_radio;
    private javax.swing.JRadioButton P_radio;
    private javax.swing.JTextField alamat_TF;
    private javax.swing.JButton batal_button;
    private javax.swing.JTextField idPasien_TF;
    public static javax.swing.JTextField idpetugasTF;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JButton keluar_button;
    private javax.swing.JButton keluar_button1;
    private javax.swing.JTextField namaOrtu_TF;
    private javax.swing.JTextField namaPasien_TF;
    private javax.swing.JTextField noTelpOrtu_TF;
    private javax.swing.JButton simpan_button;
    private com.toedter.calendar.JDateChooser tanggalLahir_DC;
    // End of variables declaration//GEN-END:variables
}
